package com.mango.leet.code.middle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: mango
 * @Date: 2024/3/21 23:33
 */
public class LC2671 {
    public static void main(String[] args) {
        FrequencyTracker ft = new FrequencyTracker();
        ft.add(5);
        ft.add(5);
        System.out.println(ft.hasFrequency(1));
        System.out.println(ft.hasFrequency(2));
        ft.add(6);
        ft.add(5);
        ft.add(1);
    }
}

class FrequencyTracker {
    Map<Integer, Integer> map;
    Map<Integer, Set<Integer>> sizeMap;
    public FrequencyTracker() {
        this.map = new HashMap<>();
        this.sizeMap = new HashMap<>();
    }

    public void add(int number) {
        int size = map.getOrDefault(number, 0) + 1;
        map.put(number, size);

        Set<Integer> set1 = sizeMap.getOrDefault(size - 1, new HashSet<>());
        set1.remove(number);
        Set<Integer> set = sizeMap.getOrDefault(size, new HashSet<>());
        set.add(number);
        sizeMap.put(size, set);
    }

    public void deleteOne(int number) {
        int size = map.getOrDefault(number, 0);
        if (size > 0) {
            Set<Integer> set = sizeMap.get(size);
            set.remove(number);

            Set<Integer> set2 = sizeMap.getOrDefault(size-1, new HashSet<>());
            set2.add(number);
            sizeMap.put(size-1,set2);
            map.put(number, size - 1);
        }
    }

    public boolean hasFrequency(int frequency) {
        return sizeMap.containsKey(frequency) && sizeMap.get(frequency).size()>0;
    }
}
